const app = getApp()
import drawQrcode from '../../utils/weapp.qrcode.esm.js'

Page({
  data: {
    banners: [
      'https://ftp.bmp.ovh/imgs/2021/04/6cdb0728fbc67ce1.jpg',
      "https://ftp.bmp.ovh/imgs/2021/04/8aed9e4482a5f56a.webp",
      "https://ftp.bmp.ovh/imgs/2021/04/f403c83cac8801f3.jpeg",
    ],
    text: '',
    todos: []
  },
  previewImage(event) {
    const self = this
    const { url } = event.currentTarget.dataset
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  onLoad() {
    // ajax
    const self = this


    drawQrcode({
      width: 200,
      height: 200,
      canvasId: 'myQrcode',
      // ctx: wx.createCanvasContext('myQrcode'),
      text: '测试小程序生成二维码',
      // v1.0.0+版本支持在二维码上绘制图片
      image: {
        imageResource: '../../images/tabbar/home.png',
        dx: 70,
        dy: 70,
        dWidth: 60,
        dHeight: 60
      }
    })

    wx.showLoading()
    wx.request({
      url: 'https://api.yizhanketang.cn/api/v1/todos',
      success(res) {
        self.setData({
          todos: res.data.data
        })
        wx.hideLoading()
        // console.log(res.data.data)
      },
      fail(res) {
        debugger
        wx.showToast({
          title: "网络错误"
        })
      }
    })
  },
  toShow(event) {
    const { id } = event.currentTarget.dataset
    console.log(id)
    wx.navigateTo({
      url: `../../views/todo/todo?id=${id}&form=home`
    })
  },
  handleClick(event) {
    const self = this
    wx.showLoading()
    wx.request({
      method: 'POST',
      url: 'https://api.yizhanketang.cn/api/v1/todos',
      data: {
        content: self.data.text
      },
      success(res) {
        console.log(res)
        wx.request({
          url: 'https://api.yizhanketang.cn/api/v1/todos',
          success(res) {
            self.setData({
              todos: res.data.data,
              text: ''
            })
            wx.hideLoading()
          },
          fail(res) {
            console.log(res)
          }
        })
      }
    })
    /*this.setData({
      // todos: this.data.todos.concat(this.data.text),
      todos: [...this.data.todos, this.data.text],
      text: '',
    })*/
    // console.log(this.data.text)
    // console.log(event)
  },
  handleChange(event) {
    this.setData({
      text: event.detail.value
    })
    // console.log(event.detail.value)
  }
})

